package com.open.shitou.presenter;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.district.DistrictResult;
import com.amap.api.services.district.DistrictSearch;
import com.amap.api.services.district.DistrictSearchQuery;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.orhanobut.logger.Logger;

import java.util.List;

import static cn.bmob.v3.Bmob.getApplicationContext;

/**
 * 定位 P层，主要为获取地址，城市等等.
 */
public class LocationPresenter {

    /**
     * 根据 城市关键字搜索下面的 城市，
     * 比如 海南，则返回 三亚市，乐东黎族自治 等等
     * @param keyWoard
     * @param cb
     */
    public void queryDistrict(final String keyWoard, final DistrictSearch.OnDistrictSearchListener cb) {
        DistrictSearch search = new DistrictSearch(getApplicationContext());
        DistrictSearchQuery query = new DistrictSearchQuery();
        query.setKeywords(keyWoard);//传入关键字
//        query.setPageSize(1000);
        query.setShowBoundary(true);//是否返回边界值
        search.setQuery(query);
        search.setOnDistrictSearchListener(new DistrictSearch.OnDistrictSearchListener() {
            @Override
            public void onDistrictSearched(DistrictResult districtResult) {
//                ArrayList<DistrictItem> districtItems = districtResult.getDistrict();
//                if (null != districtItems && districtItems.size() > 0) {
//                    for (DistrictItem item:districtItems.get(0).getSubDistrict()) {
//                        Logger.d(item);
//                    }
//                } else {
//                }
                cb.onDistrictSearched(districtResult);
            }
        });//绑定监听器
        search.searchDistrictAnsy();//开始搜索
//        search.searchDistrict();
    }

    /**
     * 搜素提示，比如乐安路或者抱由 会提示一些信息.
     */
    public void queryInputtip(
            final String keyWoard,
            final String city,
            final Inputtips.InputtipsListener cb) {
        InputtipsQuery inputquery = new InputtipsQuery(keyWoard, city);
        inputquery.setCityLimit(false); // 城市限定
        Inputtips inputTips = new Inputtips(getApplicationContext(), inputquery);
        inputTips.setInputtipsListener(new Inputtips.InputtipsListener() {
            @Override
            public void onGetInputtips(List<Tip> list, int code) {
                Logger.d(list);
                cb.onGetInputtips(list, code);
//                if (code == 1000) {
//                    //成功
//                    for (Tip tip : list) {
//                        Logger.d(tip);
//                    }
//                }else{
//                    //失败
//                }
            }
        });
        inputTips.requestInputtipsAsyn();
    }

    public void test() {
        PoiSearch.Query query1 = new PoiSearch.Query("抱由", "", "");
        PoiSearch poiSearch = new PoiSearch(getApplicationContext(), query1);
        poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int rCode) {
                Logger.d(poiResult);
            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int rCode) {
                Logger.d(poiItem);
            }
        });
        poiSearch.searchPOIAsyn();
//    poiSearch.searchPOIIdAsyn("抱由");
    }

}
